//
//  DataCenter.m
//  sign Detector
//
//  Created by Thanh Hai on 4/3/13.
//  Copyright (c) 2013 Thoi Thai. All rights reserved.
//

#import "DataCenter.h"

static DataCenter *_Share = nil;

@interface DataCenter (){
    enumAppLocking _lockStatus;
}

@end

@implementation DataCenter

+ (DataCenter*)shared{
    @synchronized([DataCenter class]){
        if (!_Share) {
            _Share = [[DataCenter alloc] init];
        }
        
        return _Share;
    }
}

- (id)init{
    if (self == [super init]) {
        //Init Code
        _lockStatus = enumAppLocking_locked;
    }
    
    return self;
}

#pragma mark - Public Methods

- (UIImage*)standardImageSign{
    NSURL *appDocumentPath = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *imagePath = [appDocumentPath URLByAppendingPathComponent:@"standard.png"];
    NSData *data = [NSData dataWithContentsOfURL:imagePath];
    if (!data) {
        return nil;
    }
    UIImage *image = [UIImage imageWithData:data];
    
    return image;
}

- (BOOL)setNewStandardSignImage:(UIImage*)image{
    if (!image) {
        return NO;
    }
    NSURL *appDocumentPath = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *imagePath = [appDocumentPath URLByAppendingPathComponent:@"standard.png"];

    NSData *data = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];
    NSError *error = nil;
    if ([data writeToURL:imagePath atomically:YES]) {
        return YES;
    }
    NSLog(@"Error : %@", [error description]);
    
    return NO;
}

#pragma mark - Lock/Unlock App

- (void)lock:(void(^)())complete{
    _lockStatus = enumAppLocking_locked;
    complete();
}

- (void)unlock:(void(^)())complete{
    _lockStatus = enumAppLocking_unlock;
    complete();
}

- (enumAppLocking)appLock{
    return _lockStatus;
}

@end
